home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / wf120.zip / WILDFILE.H < prev    next >
C/C++ Source or Header  |  1992-01-06  |  11KB  |  250 lines

  1. /*
  2.  EPSHeader
  3.  
  4.    File: wildfile.h
  5.    Author: J. Kercheval
  6.    Created: Thu, 03/14/1991  20:10:16
  7. */
  8. /*
  9.  EPSRevision History
  10.  
  11.    J. Kercheval  Thu, 03/14/1991  21:15:57  ifdef attribute constants
  12.    J. Kercheval  Sat, 03/16/1991  15:03:54  add FileInfo structure
  13.    J. Kercheval  Sat, 03/16/1991  20:08:41  add status byte
  14.    J. Kercheval  Sun, 03/31/1991  16:15:06  rename time and date defines
  15.    D. Kirschbaum Mon, 05/13/1991  16:32:00  For Turbo C v2.0:
  16.                                             Add <dos.h>
  17.                                             Change some file attribs.
  18. */
  19.  
  20. #ifndef BOOLEAN
  21. #define BOOLEAN int
  22. #define TRUE 1
  23. #define FALSE 0
  24. #endif
  25.  
  26. /*
  27.  * Dedicated to the Public Domain.  Use this for any purpose for any reason
  28.  * with no restrictions, warranties, suitability for fitness etc.
  29.  *
  30.  * Wild File is a module designed to aid the developer in producing programs
  31.  * which use a *sensible* wildcard scheme.  For instance using this module
  32.  * your command line could specify any SH style regular expression which
  33.  * produced a valid file name as input for file names.  The only limitation
  34.  * is that since DOS uses the literal escape character '\' as a delimiter to
  35.  * specifying a directory follow you may not use this character in the
  36.  * command line in this routine.
  37.  *
  38.  * This module assumes compilation on MSC or Borland C (Turbo C, TCC, BCC)
  39.  */
  40.  
  41.  
  42. /*----------------------------------------------------------------------------
  43.  *
  44.  *
  45.  * These routines allow the use of multiple search threads.  Simply declare a
  46.  * variable to be of type FileInfo (perhaps called ff), fill in the
  47.  * file_pattern and the file_attributes fields (ie.  ff.file_pattern and
  48.  * ff.file_attributes), initiate the search with find_firstfile() and
  49.  * continue the search with find_nextfile().  There is no reason why you
  50.  * could not conduct more than one search simultaneously using several
  51.  * different variables.
  52.  *
  53.  * In general you should not alter any element of the FileInfo structure once
  54.  * find_firstfile() has been called.  You should never alter any element of
  55.  * the file field in the FileInfo structure.  Doing so may make it impossible
  56.  * to continue the search.
  57.  *
  58.  * The elements of the FileInfo structure are defined as follows:
  59.  *
  60.  *    file_pattern      A string representing the search expression
  61.  *                      filled in by the caller before the call to
  62.  *                      find_firstfile()
  63.  *
  64.  *    file_attributes   An 8 bit flag representing the file attributes
  65.  *                      you are interested in.  This should be created
  66.  *                      by using the | operator and the defined
  67.  *                      constants below (ie. _FA_HIDDEN | _FA_NORMAL |
  68.  *                      _FA_SYSTEM would obtain all files that were
  69.  *                      hidden, system or normal (no attribute) files).
  70.  *                      This should be filled before the call to
  71.  *                      find_firstfile()
  72.  *
  73.  *    file_path         Derived from the file_pattern feild in
  74.  *                      find_firstfile(), this feild represents the
  75.  *                      path required to locate the file whose name is
  76.  *                      found in file.name as described below
  77.  *
  78.  *    file              This is a structure defined by most compilers
  79.  *                      and its contents are DOS defined.  It is filled
  80.  *                      on a call to find_firstfile() or find_nextfile()
  81.  *                      and contains specific information about each
  82.  *                      file found.  If find_firstfile() or
  83.  *                      find_nextfile() return FALSE then this
  84.  *                      structure has undefined fields.
  85.  *                      The structures elements are defined as:
  86.  *
  87.  *                          file.name       The name and extension of
  88.  *                                          the last file found. Is of
  89.  *                                          type char[13].
  90.  *
  91.  *                          file.size       The size in bytes of the
  92.  *                                          last file found. Is of type
  93.  *                                          long.
  94.  *
  95.  *                          file.attributes The ORed attributes of the
  96.  *                                          last found file. Is of type
  97.  *                                          char.
  98.  *
  99.  *                          file.datestamp  The data of last modification
  100.  *                                          for the last found file. Is
  101.  *                                          of type int with bits defined
  102.  *                                          as follows:
  103.  *                                              bits 0-4    day
  104.  *                                              bits 5-8    month
  105.  *                                              bits 9-15   years since 1980
  106.  *
  107.  *                          file.timestamp  The time of last modification
  108.  *                                          for the last found file.  Is
  109.  *                                          if type int with bits defined
  110.  *                                          as follows:
  111.  *                                              bits 0-4    seconds div 2
  112.  *                                              bits 5-10   minutes
  113.  *                                              bits 11-15  hours
  114.  *
  115.  *    status            Information flags used internally by this module.
  116.  *                      There is no user interesting information and this
  117.  *                      status byte should not be altered.  Changing this
  118.  *                      field may result in incorrect file matching behavior
  119.  *                      by find_nextfile().
  120.  *
  121.  * As an example use of these routines refer to the main() function at the
  122.  * end of wildfile.c
  123.  *
  124.  ---------------------------------------------------------------------------*/
  125.  
  126. #ifdef __TURBOC__
  127. #include <dir.h>
  128. #include <dos.h>                /* v1.14 for FA_RDONLY, etc. */
  129. #define info ffblk
  130. #define attributes ff_attrib
  131. #define timestamp ff_ftime
  132. #define datestamp ff_fdate
  133. #define size ff_fsize
  134. #define name ff_name
  135. #else
  136. #include <dos.h>
  137. #define info find_t
  138. #define attributes attrib
  139. #define timestamp wr_time
  140. #define datestamp wr_date
  141. #define size size
  142. #define name name
  143. #define MAXPATH   80
  144. #endif
  145.  
  146. struct file_info_struct {
  147.  
  148.     /* thest elements should be set before the call to find_firstfile() */
  149.     char file_pattern[2 * MAXPATH + 1]; /* the original file pattern */
  150.     char file_attributes;       /* the wanted file attributes */
  151.  
  152.     /* these elements are filled by find_firstfile() and find_nextfile() */
  153.     char file_path[MAXPATH + 1];/* the lead portion of the path */
  154.     struct info file;           /* the file block */
  155.     unsigned char status;       /* internal status information */
  156. };
  157.  
  158. typedef struct file_info_struct FileInfo;
  159.  
  160.  
  161. /*----------------------------------------------------------------------------
  162.  *
  163.  * In behavior regarding attributes, these routines differ a bit from the
  164.  * standard DOS calls to findfile and findnext.  These routines will only
  165.  * return files with those attributes you explicitly set.  The standard DOS
  166.  * call will return all normal files in addition to the attributes wanted.
  167.  * This is a little silly since the behavior is true for only certian
  168.  * attribute bits.  The Volume bit set will not result in the return of
  169.  * normal files.  One side effect of this design decision is that you must
  170.  * explicitly ask for files with normal or *no* attributes.  Believe me this
  171.  * is NOT a hardship.
  172.  *
  173.  * To obtain a combination of attributes just or the attribute constants as
  174.  * needed (ie. _FA_NORMAL | _FA_READONLY).  Only those files with a wanted
  175.  * attribute will be returned.  If files without any attributes are wanted
  176.  * then you must or the A_NORMAL constant into the attr parameter (This is
  177.  * different behavior than the standard findfirst, findnext call behavior).
  178.  *
  179.  * Note: These constants are defined internally for portability.  The
  180.  * constant used does not change but the various compilers sure have invented
  181.  * enough names for them.  I assume BC (Turbo C) or MSC.
  182.  *
  183.  * The file attribute definitions are as follows:
  184.  *
  185.  *    _FA_READONLY   Search for readonly files
  186.  *    _FA_HIDDEN     Search for hidden files
  187.  *    _FA_SYSTEM     Search for system files
  188.  *    _FA_VOLUME     Search for Volume label
  189.  *    _FA_DIRECTORY  Search for Directory
  190.  *    _FA_ARCHIVE    Search for non backup up files
  191.  *    _FA_NORMAL     Search for all normal files
  192.  *
  193.  ---------------------------------------------------------------------------*/
  194.  
  195. /* if not using Borland C then assume MSC */
  196.  
  197. #ifdef __TURBOC__
  198. #define _FA_READONLY  FA_RDONLY /* FA_READONLY v1.14 */
  199. #define _FA_HIDDEN    FA_HIDDEN
  200. #define _FA_SYSTEM    FA_SYSTEM
  201. #define _FA_VOLUME    FA_VOLUME
  202. #define _FA_DIRECTORY FA_DIREC  /* FA_DIRECTORY v1.14 */
  203. #define _FA_ARCHIVE   FA_ARCH   /* FA_ARCHIVE v1.14 */
  204. #define _FA_NORMAL    0x40
  205. #else
  206. #define _FA_READONLY  _A_RDONLY
  207. #define _FA_HIDDEN    _A_HIDDEN
  208. #define _FA_SYSTEM    _A_SYSTEM
  209. #define _FA_VOLUME    _A_VOLID
  210. #define _FA_DIRECTORY _A_SUBDIR
  211. #define _FA_ARCHIVE   _A_ARCH
  212. #define _FA_NORMAL    0x40
  213. #endif
  214.  
  215.  
  216. /*----------------------------------------------------------------------------
  217.  *
  218.  * find_firstfile() will find the first file matching file_pattern.
  219.  * find_nextfile() will find the next file matching the name specified by
  220.  * find_firstfile().  The routines will return the BOOLEAN value FALSE if the
  221.  * file is not found in the call to find_firstfile() or if there are no more
  222.  * files found in a call to find_nextfile.  If a file is found then both
  223.  * routines will return the BOOLEAN value TRUE.
  224.  *
  225.  * file_pattern may be SH style regular expressions.  The regular expression
  226.  * symbols allowed are:
  227.  *
  228.  *       '*' -- Matches any set of zero or more characters
  229.  *       '?' -- Matches any single character
  230.  *       [SET] matches any character in the specified set,
  231.  *       [!SET] or [^SET] matches any character not in the specified set.
  232.  *       \ is allowed within a set to escape a character like ']' or '-'
  233.  *
  234.  * A set is composed of characters or ranges; a range looks like character
  235.  * hyphen character (as in 0-9 or A-Z).  [0-9a-zA-Z_] is the minimal set of
  236.  * characters allowed in the [..] pattern construct.  Other characters are
  237.  * allowed (ie. 8 bit characters) if your system will support them.
  238.  *
  239.  * To suppress the special syntactic significance of any of `[]*?!^-\' within
  240.  * an [..] construct and match the character exactly, precede it with a `\'.
  241.  *
  242.  * Parameters:
  243.  *       ff - a pointer to a FileInfo structure already allocated or
  244.  *            declared.
  245.  *
  246.  ---------------------------------------------------------------------------*/
  247.  
  248. BOOLEAN find_firstfile(FileInfo * ff);
  249. BOOLEAN find_nextfile(FileInfo * ff);
  250.